// pages/apply_search_result/index.js
const app = getApp()
import { getMatchList } from '../../api/match.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showToastState:true,//是否已加载全部
    searchValue:'',
    list:[],
    pageNum: 1,
    pageSize: 10,
    isRequst:false
    
  },
  //取消
  cancel(){
    wx.navigateBack(1)
  },
  bindKeyInput: function (e) {
    let value = e.detail.value
     value = value.replace(/\s+/g, "")
    this.setData({
      searchValue:value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ searchValue:options.value})
  },


  //筛选条件变化时重置以下数据
  feach() {
    this.setData({ list: [], pageNum: 1, isRequst: false ,showToastState:true})
    this._getMatchList()
  },
  // 获取赛程列表
  _getMatchList: function () {
    if (!this.data.isRequst) {
      let { pageNum, pageSize, searchValue, list } = this.data
        getMatchList({ pageNum, pageSize, nameOrAddress: searchValue }).then(res => {
          let arr = res.rows;
          if (arr.length > 0) {
              for (let i in arr) {
                arr[i].date = app.timeFn(arr[i].timeStart,'-')[4]
                arr[i].day = arr[i].day.replace(/-/g,'/')
                arr[i].shortAddress = arr[i].shortAddress.replace(/,/g,'/')
              }
          this.setData({ list: [...list, ...arr], pageNum: pageNum + 1 })
          if(this.data.list.length==res.total){
            this.setData({showToastState:false})
          }else{
            this.setData({showToastState:true})
          }
        } else {
            this.setData({showToastState:false})
        }
        this.setData({ isRequst: false })
      })
    }
  },

  //点击去详情
  goto(e){
    wx.navigateTo({url: '/pages/competition_detail/index?id=' + e.currentTarget.dataset.id})
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取当前时间
    this.feach()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this._getMatchList()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})